package com.apes.hr.paySupport.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.plugin.soe.annotations.State;
import com.apes.framework.plugin.soe.annotations.States;
import com.apes.framework.util.ApesConst;
import com.apes.framework.util.DateUtil;
import com.apes.hr.base.model.Employee;
import com.apes.hr.base.model.Job;
import com.apes.hr.base.model.WorkPost;
import com.apes.scm.masterdata.enumeration.model.Enumeration;
import com.apes.scm.masterdata.frame.model.Branch;
import com.apes.scm.masterdata.frame.model.Dept;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.Date;

/**
 * @Title: SupportApplicationAffirm
 * @Description:付费支援申请单（生成确认单）
 * @author: ZhengZeXian
 * @create: 2021/4/15 10:18
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = SupportApplicationAffirm.CONST_TABLE_NAME)
public class SupportApplicationAffirm extends Model{

    public static final String CONST_TABLE_NAME = "hr_support_application_affirm";

    @Id
    @Column(name = "id")
    @ApesGenerator(code = CONST_TABLE_NAME, name = "付费支援申请单确认单编码", padding = 15)
    @Comment("编码")
    private String id;

    @Column(name = "request_code", length = 250)
    @Comment("申请单号")
    private String requestCode;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "support_uid", referencedColumnName = "id")
    @Comment("人员编码")
    private Employee supportUid;

    @States(name = "单据状态", values = {
            @State(id = "create", name = "创建"),
            @State(id = "confirm", name = "确认"),
    })
    @Column(name = "state", length = 16, nullable = false)
    @Comment("单据状态")
    private String state;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "dept_id", referencedColumnName = "id")
    @Comment("门店名称")
    private Dept dept;

    @Column(name = "support_amt", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("总计支援金额")
    private double supportAmt = 0;

    @Column(name = "subsidy_amt", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("总计补贴金额")
    private double subsidyAmt = 0;

    @Column(name = "subsidy_amt_day", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("补贴金额/天")
    private double subsidyAmtDay = 0;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "branch_id", referencedColumnName = "id")
    @Comment("支援前部门")
    private Branch branch;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "branch_operation_id", referencedColumnName = "id")
    @Comment("支援前运营模式")
    private Enumeration branchOperation;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "branch_id_after", referencedColumnName = "id")
    @Comment("支援后部门")
    private Branch branchAfter;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "branch_after_operation_id", referencedColumnName = "id")
    @Comment("支援后运营模式")
    private Enumeration branchAfterOperation;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "branch_fn_tag_id", referencedColumnName = "id")
    @Comment("赋能标签")
    private Enumeration branchFnTag;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "work_post_id", referencedColumnName = "id")
    @Comment("调动前岗位")
    private WorkPost workPost;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "work_post_id_after", referencedColumnName = "id")
    @Comment("调动后岗位")
    private WorkPost workPostAfter;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "job_id_after", referencedColumnName = "id")
    @Comment("调动后职位")
    private Job jobAfter;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "transfer_kind", referencedColumnName = "id")
    @Comment("支援性质")
    private Enumeration hrTransferKind;    //开店支援，同城支援，出差支援，开店出差支援

    @JSONField(format = DateUtil.SHOW_DATE_FORMAT)
    @Column(name = "start_date")
    @Comment("支援开始日期")
    private Date startDate;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "start_time",referencedColumnName = "id")
    @Comment("支援开始时间")
    private Enumeration startTime; // AM为上午 PM为下午

    @JSONField(format = DateUtil.SHOW_DATE_FORMAT)
    @Column(name = "end_date")
    @Comment("支援结束日期")
    private Date endDate;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "end_time",referencedColumnName = "id")
    @Comment("支援结束时间")
    private Enumeration endTime; // AM为上午 PM为下午

    @JSONField(format = DateUtil.SHOW_DATE_FORMAT)
    @Column(name = "confirm_date")
    @Comment("确认支援日期")
    private Date confirmDate;

    @Column(name = "support_days", length = 50, nullable = false)
    @Comment("应支援天数")
    private String supportDays;

    @Column(name = "actual_support_days", length = 50, nullable = false)
    @Comment("实际支援的天数")
    private String actualSupportDays;

    @Column(name = "not_support_days", length = 50, nullable = false)
    @Comment("没有支援的天数")
    private String notSupportDays;

    @Column(name = "note", length = 250)
    @Comment("出人方备注")
    private String note;

    @Column(name = "use_candidates", length = 4000)
    @Comment("用人门店OA候选人")
    private String useCandidates;

    @Column(name = "out_candidates", length = 4000)
    @Comment("出人门店OA候选人")
    private String outCandidates;

    @Column(name = "support_oa_uid", length = 4000)
    @Comment("支援人OA")
    private String supportOAUid;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "supportApplication_item_id", referencedColumnName = "id")
    private SupportApplicationItem supportApplicationItem;

    @Transient
    private String processInitiator;

    @Transient
    private String sign;

    @Transient
    private Double supportAmtDay;

//    @Transient
//    private Double subsidyAmtDay;
//
//    public Double getSubsidyAmtDay() {
//        return this.subsidyAmt / Double.parseDouble(actualSupportDays);
//    }

    public Double getSupportAmtDay() {
        return supportApplicationItem.getDiscountsAmt();
    }

    @Column(name = "expense_id", length = 4000)
    @Comment("扣收的费用单编码")
    private String expenseId;

    @Column(name = "give_expense_id", length = 4000)
    @Comment("给出的费用单编码")
    private String giveExpenseId;

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Column(name = "finish_date")
    @Comment("完成日期")
    private Date finishDate;
}


